// middleware/auth.js
const jwt = require('jsonwebtoken');

const auth = (req, res, next) => {
  // console.log(req.header('Authorization'))
  if(!req.header('Authorization')){
    res.status(400).json({ message: '缺少请求头：token为空' });
  }
  const token = req.header('Authorization').replace('Bearer ', '');
  // console.log('token:::',token)
  try {
    const decoded = jwt.verify(token, '39qw89r23890');
    // console.log("decoded:::",decoded)
    req.user = decoded;
    
    next();
  } catch (error) {
    res.status(400).json({ message: 'Authentication failed' });
  }
};

module.exports = auth;